package com.eduadmin.controller;

import com.eduadmin.entity.Page;
import com.eduadmin.entity.Score;
import com.eduadmin.entity.TotalDivide;
import com.eduadmin.service.ITotalDivideService;
import com.eduadmin.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Controller
public class TotalDivideController {
    private final ITotalDivideService totalDivideService;
    @Autowired
    public TotalDivideController(ITotalDivideService totalDivideService) {
        this.totalDivideService = totalDivideService;
    }
    // 分页显示
    @RequestMapping(value = "getQueTotalDivide")
    @ResponseBody
    public Map<String, Object> getQueTotalDivide(
            @RequestParam(value = "page", required = false) String page,
            @RequestParam(value = "rows", required = false) String rows,
            @RequestParam("gradeName") String gradeName,
            @RequestParam("semesterName") String semesterName,
            @RequestParam("examName") String examName,
            @RequestParam("categoryName") String categoryName){

        Page pageBean = new Page(Integer.parseInt(page), Integer.parseInt(rows));
        Map<String, Object> reMap = new HashMap<>();                 //查询结果map
        Map<String, Object> paraMap = new HashMap<>();           //查询参数map
        paraMap.put("offset", pageBean.getOffset());
        paraMap.put("rows", pageBean.getRows());
        paraMap.put("gradeName",gradeName);
        paraMap.put("semesterName",semesterName);
        paraMap.put("examName",examName);
        paraMap.put("categoryName",categoryName);

        List<TotalDivide> totalDivideList=totalDivideService.getQueTotalDivideByParams(paraMap);

        paraMap.clear();
        paraMap.put("gradeName",gradeName);
        paraMap.put("semesterName",semesterName);
        paraMap.put("examName",examName);
        paraMap.put("categoryName",categoryName);

        int total = totalDivideService.getQueTotalDivideNumberByParams(paraMap);//不带分页限制的总记录条数。
        reMap.put("rows", totalDivideList);
        reMap.put("total", total);
        return reMap;
    }
    // 导出到excel
    @RequestMapping("exportTotalDivideData")
    public void exportTotalDivideData(@RequestParam("gradeName") String gradeName,
                                      @RequestParam("semesterName") String semesterName,
                                      @RequestParam("examName") String examName,
                                      @RequestParam("categoryName") String categoryName,
                                      HttpServletResponse response){
        Map<String, Object> paraMap = new HashMap<>();           //查询参数map
        paraMap.put("gradeName",gradeName);
        paraMap.put("semesterName",semesterName);
        paraMap.put("examName",examName);
        paraMap.put("categoryName",categoryName);
        List<TotalDivide> totalDivideList=totalDivideService.getQueTotalDivideForChartsByParams(paraMap); //返回值已经按照班级升序，总成绩降序排序
        try {
            ExcelUtils.export2Web(response, gradeName+semesterName+examName+"总分一分段表", "总分一分段表", TotalDivide.class, totalDivideList);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // 图表应用
    @RequestMapping(value = "getTotalDivideChartData")
    @ResponseBody
    public Map<String,Object> getTotalDivideChartData(
            @RequestParam("gradeName") String gradeName,
            @RequestParam("semesterName") String semesterName,
            @RequestParam("examName") String examName,
            @RequestParam("categoryName") String categoryName){
        Map<String, Object> paraMap = new HashMap<>();           //查询参数map
        paraMap.put("gradeName",gradeName);
        paraMap.put("semesterName",semesterName);
        paraMap.put("examName",examName);
        paraMap.put("categoryName",categoryName);
        List<TotalDivide> totalDivideList=totalDivideService.getQueTotalDivideForChartsByParams(paraMap); //返回值已经按照班级升序，总成绩降序排序

        // 计算总分的分数区间[minn,maxn]，[班级名称]，[[[总分段1,人数1占比],[总分段2，人数2占比]],[[,],[,]]]数据
        List<String> forLegendData=new ArrayList();
        int maxn=0,minn=100000;
        for(int i=0;i<totalDivideList.size();i++){
            int t=totalDivideList.get(i).getTotalDivide();
            if (maxn<t) maxn=t;
            if (minn>t) minn=t;
            String tTeamName=totalDivideList.get(i).getTeamName();
            boolean tag=true;
            for (int j=0;j<forLegendData.size();j++){
                if (forLegendData.get(j).equals(tTeamName)) {tag=false;break;}
            }
            if (tag) {forLegendData.add(tTeamName);};
        }
        // 获取班级实考总人数。
        int studentTotal[]=new int[100];
        int si=0;
        for(int i=0;i<totalDivideList.size();i++){
            if (totalDivideList.get(i).getTotalDivide()==minn) {
                studentTotal[si]=totalDivideList.get(i).getTeamStudentNumber();si++;} //最小值所在记录中有第si班参考学生人数。
        }

        // 获取每班的班级总分（共分为m段）百分比
        int m=6;
        int step=Math.round((maxn-minn+1)/m);// 取整数分数为一段
        Map<String,Object> forSeriesData=new HashMap<>();
        for (int i=0;i<forLegendData.size();i++){
            List<float[]> t0=new ArrayList<>();
            String tTeamName=forLegendData.get(i);
            // 起点为i*(maxn-minn+1)
            int j=i*(maxn-minn+1);
            for (int k=0;k<m;k++){
                float tx=totalDivideList.get(j).getTotalDivide();
                float ty=totalDivideList.get(j).getTeamStudentNumber();
                if (k==0) { //班级起始点
                     float[] t1=new float[2];
                         t1[0]=tx;
                         if (studentTotal[i]>0) // 防止除数为0
                            t1[1]=Float.parseFloat(new Formatter().format("%.2f", ty/studentTotal[i]*100).toString());//保留小数点后两位
                         else t1[1]=0;
                         t0.add(t1);
                    } else { //中间段
                        float[] t1=new float[2];
                        ty-=totalDivideList.get(j-step).getTeamStudentNumber();
                        ty=ty>0?ty:0; //  防止人数为负数
                        t1[0]=tx;
                        if (studentTotal[i]>0) // 防止除数为0
                            t1[1]=Float.parseFloat(new Formatter().format("%.2f", ty/studentTotal[i]*100).toString());//保留小数点后两位
                        else t1[1]=0;
                        t0.add(t1);
                    }
                j+=step;
            }
            // 班级终点
            j-=step;//最后一段的起点。
            float[] t1=new float[2];
            float tx=totalDivideList.get((maxn-minn+1)*(i+1)-1).getTotalDivide();
            float ty=totalDivideList.get((maxn-minn+1)*(i+1)-1).getTeamStudentNumber()-totalDivideList.get(j).getTeamStudentNumber();
            t1[0]=tx;
            if (studentTotal[i]>0)
                t1[1]=Float.parseFloat(new Formatter().format("%.2f", ty/studentTotal[i]*100).toString());//保留小数点后两位
            else t1[1]=0;
            t0.add(t1);
            forSeriesData.put(tTeamName,t0);
        }
        Map<String,Object> res=new HashMap<>();
        res.put("forLegendData",forLegendData);
        res.put("forSeriesData",forSeriesData);
        return res;
    }
}
